﻿using GGGK.Film.IRepository;
using GGGK.Film.IService;
using GGGK.Film.Models.Dto;
using GGGK.Film.Models.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GGGK.Film.Service
{
    public class MenuInfoService : BaseService<Base_MenuInfo>, IMenuInfoService
    {
        private readonly IMenuInfoRepository repository;

        public MenuInfoService(IMenuInfoRepository repository, IBaseQueryRepository baseQuery) : base(repository, baseQuery)
        {
            this.repository = repository;
        }

        public IEnumerable<MenuInfoDto> MenuReduct(string menuName = "", int pId = 0)
        {
            var data = repository.Query(o => o.PId == pId).ToArray();
            List<MenuInfoDto> result = new List<MenuInfoDto>();
            foreach (var menu in data)
            {
                var dto = new MenuInfoDto()
                {
                    id = menu.Id,
                    menuName = menu.MenuName,
                    menuIcon = menu.MenuIcon ?? "",
                    menuUrl = menu.MenuUrl,
                    isShow = menu.IsShow,
                    pId = menu.PId,
                    menuOrder = menu.MenuOrder,
                    status = menu.Status,
                    children = MenuReduct(menuName, menu.Id)
                };

                if (dto.children.Count() == 0 && !dto.menuName.Contains(menuName))
                {
                    continue;
                }

                result.Add(dto);
            }

            return result;
        }
    }
}
